package com.senseuni.da.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.senseuni.da.R
import com.senseuni.da.databinding.FragmentSlideBinding
import com.senseuni.da.ui.dialog.DurationSettingDialog
import com.senseuni.da.utils.SpUtil


class SlideFragment : Fragment() {

    companion object {
        fun newInstance() = SlideFragment()
    }

    private lateinit var binding: FragmentSlideBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentSlideBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.sivSlideDuration.setOnClickListener {
            val dialog = DurationSettingDialog(requireContext())
            dialog.setOnResultListener { _, _ -> updateDuration() }
            dialog.create().show()
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        updateDuration()
    }

    private fun updateDuration() {
        val duration = SpUtil.getImageDuration()
        binding.sivSlideDuration.setSummary(getString(R.string.slide_duration_tip, duration))
    }

}